package org.example.websocket.controller;




import org.example.websocket.component.WebSocketServer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/****
 * @author songshiming
 * @date 2023/1/3
 * @desc
 */

@Controller
@RequestMapping("/")
public class WebSocketController
{

    //推送数据接口
    @ResponseBody
    @RequestMapping("/push/{cid}")
    public Map<String, Object> pushToWeb(@PathVariable String cid, String message) {
        if (message == null) {
            message = "我是消息!!!";
        }
        Map<String, Object> result = new HashMap<>();
        try {
            WebSocketServer.sendInfo(message, cid);
            result.put("code", 200);
            result.put("msg", "success");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

}
